package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.HomeBean;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.TDevice;

/**
 * Created by Administrator on 2016/8/17 0017.
 */
public class FeatureAdapter extends RecyclerView.Adapter<FeatureAdapter.MyHolder> {
    private Context context;
    List<HomeBean.PromotionListBean> promotionList;
    private LayoutInflater inflater;

    public FeatureAdapter(Context context, List<HomeBean.PromotionListBean> promotionList) {
        this.context = context;
        this.promotionList = promotionList;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_feature_goods, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        int width = (int)(TDevice.getScreenWidth()-80)/3;
        int height = (int)(TDevice.getScreenWidth()-90)/3;
//        Log.i("aaa","--a--"+width);
//        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(width,width);
        holder.imageFeature.getLayoutParams().width=width;
        holder.imageFeature.getLayoutParams().height=width;


            String []nUrl=promotionList.get(0).getGoodsList().get(position).getHeadImg().split("\\.");
            StringBuffer buffer=new StringBuffer();
        buffer.append(nUrl[0]).append("_360.").append(nUrl[1]);
//        if(nUrl[1].equals("jpg")){
//            buffer.append(nUrl[0]).append("_360.jpg");
//        }else if(nUrl[1].equals("png")){
//            buffer.append(nUrl[0]).append("_360.png");
//        }else if(nUrl[1].equals("jpeg")){
//            buffer.append(nUrl[0]).append("_360.jpeg");
//        }
        Glide.with(context).load(OKUtilAPI.imgBaseUrl+buffer).into(holder.imageFeature);
        holder.textFeature.setText(promotionList.get(0).getGoodsList().get(position).getName());
        holder.textFeatureprice.setText("￥ "+ NetUtils.setDecimal(promotionList.get(0).getGoodsList().get(position).getPriceFlash()));

//        Log.e("Text","textFeature"+goodsList.get(0).getName());

        holder.llRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });

        holder.llRoot.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemLongClick(v, position);
                }
                return true;
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }

    @Override
    public int getItemCount() {
        return promotionList.get(0).getGoodsList().size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private ImageView imageFeature;
        private TextView textFeature;
        private TextView textFeatureprice;
        private LinearLayout llRoot;

        public MyHolder(View itemView) {
            super(itemView);
            imageFeature = ((ImageView) itemView.findViewById(R.id.imageFeature));
            textFeature = ((TextView) itemView.findViewById(R.id.textFeature));
            textFeatureprice = ((TextView) itemView.findViewById(R.id.textFeatureprice));
            llRoot = ((LinearLayout) itemView.findViewById(R.id.llRoot));

        }
    }
}
